//
//  registViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/11.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "registViewController.h"
#import "UIView+Common.h"
#import "Masonry.h"
#import "loginViewController.h"
@interface registViewController ()

@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *yzmTextField;
@property (nonatomic, strong) UITextField *invitationTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *buttonYZM;


@end

@implementation registViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"注册"];
    self.view.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    UIView *viewOfName = [[UIView alloc] init];
    [self.view addSubview:viewOfName];
    viewOfName.layer.borderWidth = 1.0f;
    viewOfName.layer.borderColor = [UIColor blackColor].CGColor;
    [viewOfName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(30);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    
    self.nameTextField = [[UITextField alloc] init];
    [viewOfName addSubview:self.nameTextField];
    [self.nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfName).with.offset(5);
        make.left.equalTo(viewOfName).with.offset(10);
        make.right.equalTo(viewOfName).with.offset(-10);
        make.height.offset(30);
        
    }];
    
    self.nameTextField.placeholder = @"请输入用户名";
    self.nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIView *viewOfPhone = [[UIView alloc] init];
    [self.view addSubview:viewOfPhone];
    viewOfPhone.layer.borderWidth = 1.0f;
    viewOfPhone.layer.borderColor = [UIColor blackColor].CGColor;
    [viewOfPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfName.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    
    self.phoneTextField = [[UITextField alloc] init];
    [viewOfPhone addSubview:self.phoneTextField];
    self.phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.phoneTextField.placeholder = @"请输入手机号";
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfPhone).with.offset(5);
        make.left.equalTo(viewOfPhone).with.offset(10);
        make.right.equalTo(viewOfPhone).with.offset(-10);
        make.height.offset(30);
        
    }];
    //验证码
    UIView *viewYZM = [[UIView alloc] init];
    [self.view addSubview:viewYZM];
    viewYZM.layer.borderWidth = 1.0f;
    viewYZM.layer.borderColor = [UIColor blackColor].CGColor;
    [viewYZM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfPhone.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    
    self.yzmTextField = [[UITextField alloc] init];
    [viewYZM addSubview:self.yzmTextField];
    [self.yzmTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewYZM).with.offset(5);
        make.left.equalTo(viewYZM).with.offset(10);
        make.width.offset(180);
        make.height.offset(30);
        
    }];
    
    self.yzmTextField.placeholder = @"请输入验证码";
    self.yzmTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    

    self.buttonYZM = [[UIButton alloc] init];
    [viewYZM addSubview:self.buttonYZM];
    [self.buttonYZM setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.buttonYZM.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.buttonYZM setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.buttonYZM addTarget:self action:@selector(yzmButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonYZM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewYZM).with.offset(5);
        make.right.equalTo(viewYZM).with.offset(-10);
        make.left.equalTo(self.yzmTextField.mas_right).with.offset(10);
        make.height.offset(30);
        
    }];
    UILabel *labelOfOne = [[UILabel alloc] init];
    [viewYZM addSubview:labelOfOne];
    labelOfOne.backgroundColor = [UIColor colorWithRed:206 / 255.0f green:206 / 255.0f blue:206 / 255.0f alpha:1];
    [labelOfOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewYZM).with.offset(5);
        make.width.offset(1);
        make.left.equalTo(self.yzmTextField.mas_right).with.offset(1);
        make.height.offset(30);
        
    }];
    
    
    
    UIView *viewOfInvitation = [[UIView alloc] init];
    [self.view addSubview:viewOfInvitation];
    viewOfInvitation.layer.borderWidth = 1.0f;
    viewOfInvitation.layer.borderColor = [UIColor blackColor].CGColor;
    [viewOfInvitation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewYZM.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    
    self.invitationTextField = [[UITextField alloc] init];
    [viewOfInvitation addSubview:self.invitationTextField];
    self.invitationTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.invitationTextField.placeholder = @"请输入邀请码";
    [self.invitationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfInvitation).with.offset(5);
        make.left.equalTo(viewOfInvitation).with.offset(10);
        make.right.equalTo(viewOfInvitation).with.offset(-10);
        make.height.offset(30);
        
    }];
//passwordTextField
    UIView *viewOfPassword = [[UIView alloc] init];
    [self.view addSubview:viewOfPassword];
    viewOfPassword.layer.borderWidth = 1.0f;
    viewOfPassword.layer.borderColor = [UIColor blackColor].CGColor;
    [viewOfPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfInvitation.mas_bottom).with.offset(10);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    
    self.passwordTextField = [[UITextField alloc] init];
    [viewOfPassword addSubview:self.passwordTextField];
    self.passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTextField.placeholder = @"设置密码";
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfPassword).with.offset(5);
        make.left.equalTo(viewOfPassword).with.offset(10);
        make.right.equalTo(viewOfPassword).with.offset(-10);
        make.height.offset(30);
        
    }];
    
    
    UIButton *confirmButton=[[UIButton alloc] init];
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewOfPassword.mas_bottom).with.offset(30);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.offset(40);
        
    }];
    confirmButton.backgroundColor=[UIColor blackColor];
    [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
//    UIView *phoneView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, (60), (300), (45))];
//    phoneView.layer.borderWidth=(1.0);
//    [self.view addSubview:phoneView];
//    
//    phoneTextField=[[UITextField alloc]initWithFrame:CGRectMake((20), (15), (260), (14))];
//    phoneTextField.placeholder=@"请输手机号";
//    phoneTextField.keyboardType=UIKeyboardTypeDefault;
//    phoneTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
//    [phoneView addSubview:phoneTextField];
//    
//    UIView *yzmView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, (60+45+16), (300), (45))];
//    yzmView.layer.borderWidth=(1.0);
//    [self.view addSubview:yzmView];
//    
//    yzmTextField=[[UITextField alloc]initWithFrame:CGRectMake((20), (15), (260-120), (14))];
//    yzmTextField.placeholder=@"请输入验证码";
//    yzmTextField.keyboardType=UIKeyboardTypeDefault;
//    yzmTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
//    [yzmView addSubview:yzmTextField];
//    
//    UIView *xianView=[[UIView alloc]initWithFrame:CGRectMake(yzmView.right-(110)-phoneView.left, (7), (1), (30))];
//    xianView.backgroundColor=[UIColor lightGrayColor];
//    [yzmView addSubview:xianView];
//    
//    UIButton *yzmButton=[[UIButton alloc]initWithFrame:CGRectMake(yzmView.right-(120)-phoneView.left, (10), (110), (25))];
////    yzmButton.backgroundColor=[UIColor redColor];
//    [yzmButton setTitle:@"获取验证码" forState:UIControlStateNormal];
//    yzmButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//    [yzmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [yzmButton addTarget:self action:@selector(yzmButtonClick) forControlEvents:UIControlEventTouchUpInside];
//    [yzmView addSubview:yzmButton];
//    
//    UIView *passwordView=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, yzmView.bottom+(16), (300), (45))];
//    passwordView.layer.borderWidth=(1.0);
//    [self.view addSubview:passwordView];
//    
//    passwordTextField=[[UITextField alloc]initWithFrame:CGRectMake((20), (15), (260), (14))];
//    passwordTextField.placeholder=@"输入邀请码";
//    passwordTextField.keyboardType=UIKeyboardTypeDefault;
//    passwordTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
//    [passwordView addSubview:passwordTextField];
//    
//    UIView *passwordView1=[[UIView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-(300))/2, passwordView.bottom+(16), (300), (45))];
//    passwordView1.layer.borderWidth=(1.0);
//    [self.view addSubview:passwordView1];
//    
//    passwordTextField1=[[UITextField alloc]initWithFrame:CGRectMake((20), (15), (260), (14))];
//    passwordTextField1.placeholder=@"设置密码";
//    passwordTextField1.keyboardType=UIKeyboardTypeDefault;
//    passwordTextField1.clearButtonMode=UITextFieldViewModeWhileEditing;
//    [passwordView1 addSubview:passwordTextField1];
//    
    
//
//    UIView *xView=[[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-49-64-(1), DEF_SCREEN_WIDTH, (1))];
//    xView.backgroundColor=[UIColor grayColor];
//    [self.view addSubview:xView];
    
    //  添加手势
    UITapGestureRecognizer *tapGesure = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(restoreKeyBoard)];
    [self.view addGestureRecognizer:tapGesure];
}

-(void)confirmButtonClick{
    NSLog(@"确认");
    
    NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
    [loginParams setValue:self.phoneTextField.text forKey:@"phone"];
    [loginParams setValue:self.yzmTextField.text forKey:@"code"];
    [loginParams setValue:self.passwordTextField.text forKey:@"password"];
    [loginParams setValue:self.invitationTextField.text forKey:@"invite"];
    [loginParams setValue:self.nameTextField.text forKey:@"user_name"];
    
    [[MNetworkHandler sharedInstance]conURL:My_Register
                                networkType:NetWorkPOST
                                     params:loginParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
//                                       NSDictionary *dict= [returnData objectForKey:@"data"];
//                                       [PublicFunction saveConfig:[dict objectForKey:@"User_name"] forKey:@"User_name"];
                                       
                                       
                                       loginViewController *vc = [[loginViewController alloc] init];
                                       [self.navigationController pushViewController:vc animated:YES];
                                       
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }else {
                                   
                                   [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                                   
                                   
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];

}

-(void)yzmButtonClick{
//    NSLog(@"获取验证码");
//            NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
//            [loginParams setValue:self.phoneTextField.text forKey:@"phone"];
//    
//            [[MNetworkHandler sharedInstance]conURL:Send_URL
//                                        networkType:NetWorkPOST
//                                             params:loginParams
//                                           delegate:nil
//                                            showHUD:NO
//                                       successBlock:^(id returnData) {
//                                           
//                                    [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
//                                           
//                                       }
//                                       failureBlock:^(NSError *error) {
//                                           NSLog(@"%@",error);
//                                           [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
//                                       }];
    
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    NSMutableDictionary *loginParams=[[NSMutableDictionary alloc]init];
    [loginParams setValue:self.phoneTextField.text forKey:@"phone"];
    
    [[MNetworkHandler sharedInstance]conURL:Send_URL
                                networkType:NetWorkPOST
                                     params:loginParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   
                               }
                               failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.buttonYZM setTitle:@"发送验证码" forState:UIControlStateNormal];
                
                self.buttonYZM.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 61;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.buttonYZM setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
                self.buttonYZM.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 手势隐藏键盘
- (void)restoreKeyBoard
{
    [self.nameTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.yzmTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    [self.invitationTextField resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
